import ProxyHandler from "../sql/ProxyHandler";
import SystemUserMapper from "../dao/SystemUserMapper";
import SystemUser from "../entity/SystemUser";
import Session from "../session/Session";
import {BrowserWindow} from "electron";
import {v4 as uuidv4} from 'uuid';
import md5 from "crypto-js/md5";

export default class SystemUserService {
    private mapper: SystemUserMapper;
    private handler: ProxyHandler<SystemUser>;

    constructor() {
        this.mapper = new SystemUserMapper()
        this.handler = new ProxyHandler<SystemUser>();
    }

    async page(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        let offset = 0, limit = 10
        if(data.current && data.size){
            offset = (data.current - 1) * data.size
            limit = data.size
        }

        let params = {
            tableName: 'system_user',
            offset: offset,
            limit: limit,
            entity: [{
                name: 'username',
                value: data.username,
                operator: 'LIKE'
            },{
                name: 'mobile',
                value: data.mobile,
                operator: 'LIKE'
            }]
        }

        const proxy = new Proxy(this.mapper.page, this.handler)
        const { rows, total } = await proxy(params)

        let result = {
            records: rows ? rows : [],
            total: total ? total : 0,
            current: data.current ? data.current : 1,
            size: limit,
            pages: Math.ceil(total / limit)
        }
        win.webContents.send('main-to-renderer', {
            name: 'operator-page-success',
            event: 'event',
            data: result
        })
    }

    async save(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemUser();
        entity.id = md5(uuidv4()).toString();
        entity.username = data.username;
        entity.password = md5(data.password).toString();
        entity.mobile = data.mobile;
        entity.realname = data.realname;
        entity.status = data.status;

        let res = await this.mapper.selectByUsername(data.username)
        if(res != null){
            win.webContents.send('main-to-renderer', {
                name: 'operator-operator-error',
                event: 'event',
                data: '用户名已存在'
            })
            return;
        }

        res = await this.mapper.selectByMobile(data.mobile)
        if(res != null){
            win.webContents.send('main-to-renderer', {
                name: 'operator-operator-error',
                event: 'event',
                data: '手机号已存在'
            })
            return;
        }

        const proxy = new Proxy(this.mapper.insert, this.handler)
        proxy(entity)
        win.webContents.send('main-to-renderer', {
            name: 'operator-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async updateById(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemUser();
        entity.id = data.id;
        entity.username = data.username;
        entity.password = md5(data.password).toString();
        entity.mobile = data.mobile;
        entity.realname = data.realname;
        entity.status = data.status;
        const proxy = new Proxy(this.mapper.updateById, this.handler)
        proxy(entity)

        win.webContents.send('main-to-renderer', {
            name: 'operator-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async deleteById(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemUser();
        entity.id = data;
        const proxy = new Proxy(this.mapper.deleteById, this.handler)
        proxy(entity)
        win.webContents.send('main-to-renderer', {
            name: 'operator-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async getByUsername(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const res = await this.mapper.selectByUsername(data.username)
        if(!res){
            win.webContents.send('main-to-renderer', {
                name: 'app-auth-failed',
                event: 'event',
                data: "账号不存在"
            })
            return
        }
        if(res.status != 1){
            win.webContents.send('main-to-renderer', {
                name: 'app-auth-failed',
                event: 'event',
                data: "账号已禁用"
            })
            return
        }
        const inputPwd = md5(data.password).toString()
        if(inputPwd != res.password){
            win.webContents.send('main-to-renderer', {
                name: 'app-auth-failed',
                event: 'event',
                data: "账号或密码错误"
            })
            return
        }

        const session = Session.getInstance()
        session.setPrincipal(res)

        win.webContents.send('main-to-renderer', {
            name: 'app-auth-success',
            event: 'event',
            data: res
        })
    }
}
